package cn.snmp.mutual.uk.beans;

import java.io.*;
import cn.snmp.mutual.uk.event.RequestPduEvent;
import cn.snmp.mutual.uk.event.RequestPduListener;
import cn.snmp.mutual.uk.stack.*;
import cn.snmp.mutual.uk.util.SnmpUtilities;

public class UsmBeingDiscoveredBean implements RequestPduListener {
    private static final String version_id = "@(#)$Id: UsmBeingDiscoveredBean.java,v 1.8 2009/03/05 15:51:42 birgita Exp $ Copyright Westhawk Ltd";
    private UsmAgent usmAgent;
    private SnmpContextv3 context;
    private SnmpContextv3Discovery discEngineIdContextIn;
    private SnmpContextv3Discovery discTimeLineContextIn;
    private SnmpContextv3 discEngineIdContextOut;
    private SnmpContextv3 discTimeLineContextOut;

    public UsmBeingDiscoveredBean(final SnmpContextv3 myContext, final UsmAgent myUsmAgent) throws IOException {
        this.usmAgent = null;
        this.context = null;
        this.usmAgent = myUsmAgent;
        this.context = myContext;
        if (AsnObject.debug > 4) {
            System.out.println(this.getClass().getName() + " Constructor:" + "usmAgent=" + this.usmAgent.toString() + "context=" + this.context.toString());
        }
        this.usmAgent.setSnmpContext(this.context);
        (this.discEngineIdContextIn = new SnmpContextv3Discovery(this.context.getSendToHostAddress(), this.context.getPort(), this.context.getBindAddress(), this.context.getTypeSocket())).setUserName("");
        this.discEngineIdContextIn.setUseAuthentication(false);
        this.discEngineIdContextIn.setUsePrivacy(false);
        this.discEngineIdContextIn.setContextEngineId(SnmpUtilities.toBytes(this.usmAgent.getSnmpEngineId()));
        this.discEngineIdContextIn.setContextName("");
        this.discEngineIdContextIn.setUsmAgent(this.usmAgent);
        (this.discTimeLineContextIn = new SnmpContextv3Discovery(this.context.getSendToHostAddress(), this.context.getPort(), this.context.getBindAddress(), this.context.getTypeSocket())).setUserName(this.context.getUserName());
        this.discTimeLineContextIn.setUseAuthentication(this.context.isUseAuthentication());
        this.discTimeLineContextIn.setAuthenticationProtocol(this.context.getAuthenticationProtocol());
        this.discTimeLineContextIn.setUserAuthenticationPassword(this.context.getUserAuthenticationPassword());
        this.discTimeLineContextIn.setUsePrivacy(this.context.isUsePrivacy());
        this.discTimeLineContextIn.setPrivacyProtocol(this.context.getPrivacyProtocol());
        this.discTimeLineContextIn.setUserPrivacyPassword(this.context.getUserPrivacyPassword());
        this.discTimeLineContextIn.setContextEngineId(SnmpUtilities.toBytes(this.usmAgent.getSnmpEngineId()));
        this.discTimeLineContextIn.setContextName("");
        this.discTimeLineContextIn.setUsmAgent(this.usmAgent);
    }

    public void addRequestPduListener(final ListeningContextPool lcontext) throws IOException {
        if (AsnObject.debug > 4) {
            System.out.println(this.getClass().getName() + ".addRequestPduListener(" + lcontext.toString() + ")");
        }
        this.discEngineIdContextIn.addRequestPduListener(this, lcontext);
        this.discTimeLineContextIn.addRequestPduListener(this, lcontext);
    }

    public void removeRequestPduListener(final ListeningContextPool lcontext) throws IOException {
        if (AsnObject.debug > 4) {
            System.out.println(this.getClass().getName() + ".removeRequestPduListener(" + lcontext.toString() + ")");
        }
        this.discEngineIdContextIn.removeRequestPduListener(this, lcontext);
        this.discTimeLineContextIn.removeRequestPduListener(this, lcontext);
    }

    public void requestPduReceived(final RequestPduEvent evt) {
        final Object src = evt.getSource();
        final int port = evt.getHostPort();
        final Pdu orgPdu = evt.getPdu();
        if (src == this.discEngineIdContextIn) {
            this.sendEngineIdReport(orgPdu, port);
        } else {
            this.sendTimeLineReport(orgPdu, port);
        }
    }

    protected void sendEngineIdReport(final Pdu orgPdu, final int port) {
        if (AsnObject.debug > 4) {
            System.out.println(this.getClass().getName() + ".sendEngineIdReport()");
        }
        try {
            if (this.discEngineIdContextOut != null) {
                this.discEngineIdContextOut.destroy();
            }
            this.discEngineIdContextOut = new SnmpContextv3(this.discEngineIdContextIn.getHost(), port, this.discEngineIdContextIn.getBindAddress(), this.discEngineIdContextIn.getTypeSocket());
            this.discEngineIdContextOut = (SnmpContextv3) this.discEngineIdContextIn.cloneParameters(this.discEngineIdContextOut);
            final Pdu repPdu = new ReportPdu(this.discEngineIdContextOut, orgPdu);
            repPdu.addOid("1.3.6.1.6.3.15.1.1.4", new AsnUnsInteger(this.usmAgent.getUsmStatsUnknownEngineIDs()));
            repPdu.send();
        } catch (IOException iexc) {
            if (AsnObject.debug > 4) {
                System.out.println(this.getClass().getName() + ".sendEngineIdReport(): " + "IOException: " + iexc.getMessage());
            }
        } catch (PduException pexc) {
            if (AsnObject.debug > 4) {
                System.out.println(this.getClass().getName() + ".sendEngineIdReport(): " + "PduException: " + pexc.getMessage());
            }
        }
    }

    protected void sendTimeLineReport(final Pdu orgPdu, final int port) {
        if (AsnObject.debug > 4) {
            System.out.println(this.getClass().getName() + ".sendTimeLineReport()");
        }
        try {
            if (this.discTimeLineContextOut != null) {
                this.discTimeLineContextOut.destroy();
            }
            this.discTimeLineContextOut = new SnmpContextv3(this.discTimeLineContextIn.getHost(), port, this.discTimeLineContextIn.getBindAddress(), this.discTimeLineContextIn.getTypeSocket());
            this.discTimeLineContextOut = (SnmpContextv3) this.discTimeLineContextIn.cloneParameters(this.discTimeLineContextOut);
            final Pdu repPdu = new ReportPdu(this.discTimeLineContextOut, orgPdu);
            repPdu.addOid("1.3.6.1.6.3.15.1.1.2", new AsnUnsInteger(this.usmAgent.getUsmStatsNotInTimeWindows()));
            repPdu.send();
        } catch (IOException iexc) {
            if (AsnObject.debug > 4) {
                System.out.println(this.getClass().getName() + ".sendTimeLineReport(): " + "IOException: " + iexc.getMessage());
            }
        } catch (PduException pexc) {
            if (AsnObject.debug > 4) {
                System.out.println(this.getClass().getName() + ".sendEngineIdReport(): " + "PduException: " + pexc.getMessage());
            }
        }
    }

    public void freeResources() {
        this.discEngineIdContextIn.destroy();
        this.discEngineIdContextIn = null;
        this.discTimeLineContextIn.destroy();
        this.discTimeLineContextIn = null;
        if (this.discEngineIdContextOut != null) {
            this.discEngineIdContextOut.destroy();
            this.discEngineIdContextOut = null;
        }
        if (this.discTimeLineContextOut != null) {
            this.discTimeLineContextOut.destroy();
            this.discTimeLineContextOut = null;
        }
    }
}
